package com.example.himalayaproject.find.broadcast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.adapter.TopAdapter;
import com.example.himalayaproject.find.broadcast.bean.Data;
import com.example.himalayaproject.find.broadcast.bean.TopRadios;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TopMoreActivity extends AppCompatActivity implements Callback<Data>, View.OnClickListener {

    private BroadcastHttpService service;
    private TopAdapter adapter;
    private List<TopRadios> tops;
    private TextView typeTextView;
    private Intent intent;
    private String type;
    private RecyclerView recycler;
    private ImageView backImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);

        intent = getIntent();
        type = intent.getStringExtra("type");

        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);
        service.getData().enqueue(this);

        tops = new ArrayList<>();
        recycler = (RecyclerView) findViewById(R.id.bct_common_recycler);
        typeTextView = (TextView) findViewById(R.id.bct_common_type);
        backImageView = (ImageView) findViewById(R.id.bct_common_back);
        adapter = new TopAdapter(this);
        recycler.setAdapter(adapter);
        typeTextView.setText(type);
        backImageView.setOnClickListener(this);

        typeTextView.setText(type);

    }

    @Override
    public void onResponse(Call<Data> call, Response<Data> response) {
//        tops.add((TopRadios) response.body().getData().getTopRadios());
        for (int i = 0; i < response.body().getData().getTopRadios().size(); i++) {
            tops.add(response.body().getData().getTopRadios().get(i));
        }
        adapter.addAll(tops);
    }

    @Override
    public void onFailure(Call<Data> call, Throwable t) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bct_common_back:
                finish();
                break;
        }
    }


}
